package quizman;

import javax.servlet.http.HttpServletRequest;

import quizman.domain.NumericQuestion;
import quizman.domain.Question;
import quizman.domain.ShortAnswerQuestion;
import quizman.mappers.NumericAnswerMapper;
import quizman.mappers.QuestionMapper;
import quizman.mappers.ShortAnswerMapper;

public class EditQuestionTS {

	public static Question execute(String quizName, String questionName, HttpServletRequest request) throws Exception {
		
		String qType = request.getParameter("questionType");
		Question q = QuestionMapper.getQuestion(quizName, questionName);
		if(qType.contains(q.getClass().getSimpleName())) {
			if(qType.contains("Numeric")) {
				NumericQuestion nq = (NumericQuestion) q;
				nq.setLowerBound(Double.parseDouble(request.getParameter("lowerBound")));
				nq.setUpperBound(Double.parseDouble(request.getParameter("upperBound")));
				NumericAnswerMapper.update(nq, nq.getLowerBound(), nq.getUpperBound());
			} else if(qType.contains("ShortAnswer")) {
				ShortAnswerQuestion saq = (ShortAnswerQuestion) q;
				saq.setAnswer(request.getParameter("answer"));
				ShortAnswerMapper.update(saq, saq.getAnswer())
			} else if(qType.contains("Multiple")) {
				
			} else {
				String msg = "Error in EditQuestionTS.execute - Question type unknown";
				throw new Exception(msg);
			}
		} else {
			String msg = "Error in EditQuestionTS.execute - Question type mismatch";
			throw new Exception(msg);
		}
		
		return q;
	}

}
